<template>
  <GcTable
    v-loading="loading"
    v-model:searchKeywords="searchKeywords"
    :pagination="pagination"
    :columns="VISITFROM_C_COLUMNS"
    :tableData="tableData"
    :filterList="filterList"
    :defaultFilter="defaultFilter"
    searchPlaceholder="请输入ip进行搜索"
    @current-change="handlerCurrentChange"
    @search="handlerSearch"
    @get-search-params="getSearchParams"
    @reset-fileds="resetSearchParams">
  </GcTable>
</template>

<script setup lang="ts" name="VisitFromC">
import { ref, reactive } from 'vue'
import { VISITFROM_C_COLUMNS } from './const'
import usePagination from '@/hooks/usePagination'
import type { VisitFromC } from '@/http/type'
import type { Pagination } from '@/@type'
import { apiGetVisitFromC } from '@/http/service'

interface ResponseVisit extends Pagination {
  dataList: Array<VisitFromC>
}
interface AdvanceSearch {
  path: string
  visitTime: null | Array<string>
}

const { pagination, loading } = usePagination()
const tableData = ref<Array<VisitFromC>>([])
const searchKeywords = ref('')
const filterList = [
  {
    name: '接口路径',
    key: 'path',
    type: 'input' as const,
    placeholder: '请输入接口路径搜索'
  },
  {
    name: '访问时间',
    key: 'visitTime',
    type: 'date' as const,
    placeholder: '请选择开始时间',
    dateType: 'datetimerange' as const,
    format: 'YYYY-MM-DD HH:mm:ss',
    valueFormat: 'YYYY-MM-DD HH:mm:ss'
  }
]
const defaultFilter = reactive<AdvanceSearch>({
  path: '',
  visitTime: null
})

/**
 * c端接口访问记录查询
 */
async function findLoginLogList () {
  const searchParams = {
    pageNo: pagination.pageNo,
    pageSize: pagination.pageSize,
    searchKeywords: searchKeywords.value,
    ...defaultFilter,
    visitTime: null,
    startTime: defaultFilter.visitTime ? defaultFilter.visitTime[0] : null,
    endTime: defaultFilter.visitTime ? defaultFilter.visitTime[1] : null
  }
  loading.value = true
  try {
    const { dataList, ...pageInfo } = await apiGetVisitFromC<ResponseVisit>(searchParams)
    tableData.value = dataList
    Object.assign(pagination, pageInfo)
  } catch (error) {
    tableData.value = []
  } finally {
    loading.value = false
  }
}
findLoginLogList()

function handlerSearch () {
  pagination.pageNo = 1
  findLoginLogList()
}

function handlerCurrentChange (val: number) {
  pagination.pageNo = val
  findLoginLogList()
}

function getSearchParams (searchParams: AdvanceSearch) {
  pagination.pageNo = 1
  Object.assign(defaultFilter, searchParams)
  findLoginLogList()
}

function resetSearchParams (searchParams: AdvanceSearch) {
  pagination.pageNo = 1
  searchKeywords.value = ''
  Object.assign(defaultFilter, searchParams)
  findLoginLogList()
}
</script>

<style lang="scss" scoped></style>
